home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / blankery / blanker / source / blankers / life / blank.c next >
C/C++ Source or Header  |  1993-08-15  |  4KB  |  144 lines

  1. /*
  2.  *    Copyright (c) 1993 Michael D. Bayne.
  3.  *    All rights reserved.
  4.  *
  5.  *    Please see the documentation accompanying the distribution for distribution and disclaimer information.
  6.  */
  7.  
  8. #include <exec/types.h>
  9. #include <exec/memory.h>
  10.  
  11. #include <intuition/intuition.h>
  12. #include <graphics/rastport.h>
  13. #include <dos/dos.h>
  14.  
  15. #include <clib/exec_protos.h>
  16. #include <clib/graphics_protos.h>
  17. #include <clib/intuition_protos.h>
  18. #include <clib/alib_protos.h>
  19.  
  20. #include "Life.h"
  21. #include "/utility.h"
  22.  
  23. struct lPrefObject {
  24.     LONG Size;
  25.     LONG Generations;
  26.     LONG Density;
  27. };
  28.  
  29. extern    struct    lPrefObject    nP;
  30. extern    ULONG            Mode, Depth;
  31. extern    UBYTE            *prefData;
  32.  
  33. VOID blank( VOID )
  34. {
  35.     struct    lPrefObject    *lP;
  36.     struct    Screen        *LScr;
  37.     struct    RastPort    *r;
  38.         LONG        iter = 0, colrand = (1L<<Depth)-1, xpos, ypos, n = 0, m = 1, Density, Size, Gens;
  39.         WORD        w, h, i, gi, li, j, gj, lj, BWid;
  40.         BYTE        **Org[2], nbrs;
  41.  
  42.     if( LifeWnd ) lP = &nP;
  43.     else lP = ( struct lPrefObject * )prefData;
  44.  
  45.     Density = lP->Density;
  46.     Size = lP->Size;
  47.     Gens = lP->Generations;
  48.     BWid = Size - 2;
  49.  
  50.     if( LScr = OpenScreenTags( 0l, SA_DisplayID, Mode, SA_Depth, Depth, SA_Quiet, TRUE, SA_Behind, TRUE,
  51.         SA_Overscan, OSCAN_TEXT, TAG_DONE )) {
  52.  
  53.         r = &( LScr->RastPort );
  54.         w = LScr->Width / Size;
  55.         h = LScr->Height / Size;
  56.  
  57.         if(( Org[n] = AllocVec( sizeof( BYTE * ) * w,  MEMF_CLEAR ))&&
  58.             ( Org[m] = AllocVec( sizeof( BYTE * ) * w, MEMF_CLEAR ))) {
  59.         for( i = 0; i < w; i++ ) {
  60.             if(!( Org[n][i] = AllocVec( h, MEMF_CLEAR ))) break;
  61.             if(!( Org[m][i] = AllocVec( h, MEMF_CLEAR ))) break;
  62.         }
  63.         if( i == w ) {
  64.             SetRGB4( &( LScr->ViewPort ), 0, 0, 0, 0 );
  65.                  for( i = 1; i < (1L<<Depth)-1; i++ ) SetRGB4( &( LScr->ViewPort ), i,
  66.                     RangeRand( 0x0A )+5, RangeRand( 0x0A )+5, RangeRand( 0x0A )+5 );
  67.             BlankMousePointer();
  68.             ScreenToFront( LScr );
  69.  
  70.             while(!( SetSignal( 0L, 0L ) & SIGBREAKF_CTRL_C )) {
  71.  
  72.                 WaitTOF();
  73.                 if( !iter ) {
  74.                     SetRast( r, 0 );
  75.                     for( xpos = 0, i = 0; i < w; i++, xpos += Size ) {
  76.                         for( ypos = 0, j = 0; j < h; j++, ypos += Size ) {
  77.                             if( RangeRand( 100 ) < Density ) {
  78.                                 Org[n][i][j] = 1;
  79.                                 SetAPen( r, RangeRand( colrand )+1 );
  80.                                 RectFill( r, xpos, ypos, xpos + BWid, ypos + BWid );
  81.                             } else Org[n][i][j] = 0;
  82.                             Org[m][i][j] = 0;
  83.                         }
  84.                     }
  85.                 }
  86.  
  87.                 for( xpos = 0, i = 0; i < w; i++, xpos += Size ) {
  88.                     for( ypos = 0, j = 0; j < h; j++, ypos += Size ) {
  89.  
  90.                         gi = ( i + 1 )%w;
  91.                         if( i ) li = ( i - 1 )%w;
  92.                         else li = w-1;
  93.                         gj = ( j + 1 )%h;
  94.                         if( j ) lj = ( j - 1 )%h;
  95.                         else lj = h-1;
  96.                         nbrs = Org[n][gi][gj];
  97.                         nbrs += Org[n][i ][gj];
  98.                         nbrs += Org[n][li][gj];
  99.                         nbrs += Org[n][gi][j ];
  100.                         nbrs += Org[n][li][j ];
  101.                         nbrs += Org[n][gi][lj];
  102.                         nbrs += Org[n][i ][lj];
  103.                         nbrs += Org[n][li][lj];
  104.  
  105.                         switch( nbrs ) {
  106.                         case 3:
  107.                             Org[m][i][j] = 1;
  108.                             if( Org[n][i][j] == 0 ) {
  109.                                 SetAPen( r, RangeRand( colrand )+1 );
  110.                                 RectFill( r, xpos, ypos, xpos + BWid, ypos + BWid );
  111.                             }
  112.                             break;
  113.                         case 2:
  114.                             Org[m][i][j] = Org[n][i][j];
  115.                             break;
  116.                         default:
  117.                             Org[m][i][j] = 0;
  118.                             if( Org[n][i][j] ) {
  119.                                 SetAPen( r, 0 );
  120.                                 RectFill( r, xpos, ypos, xpos + BWid, ypos + BWid );
  121.                             }
  122.                         }
  123.                     }
  124.                 }
  125.                 n = m;
  126.                 m = 1 - m;
  127.                 iter = (iter+1) % Gens;
  128.             }
  129.             SetSignal( 0L, SIGBREAKF_CTRL_C );
  130.         }}
  131.  
  132.         if( Org[0] ) {
  133.             for( i = 0; i < w; i++ ) if( Org[0][i] ) FreeVec( Org[0][i] );
  134.             FreeVec( Org[0] );
  135.         }
  136.         if( Org[1] ) {
  137.             for( i = 0; i < w; i++ ) if( Org[1][i] ) FreeVec( Org[1][i] );
  138.             FreeVec( Org[1] );
  139.         }
  140.         UnblankMousePointer();
  141.         CloseScreen( LScr );
  142.         }
  143. }
  144.